package com.fuyd.pms.entity.account;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fuyd.pms.entity.Merchant;
import com.fuyd.pms.entity.base.BaseEntityGen;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.Length;


import javax.persistence.*;
import java.util.List;

/**
 * 商家用户
 * Created by fuyongde on 2015/12/23.
 */
@Entity
@Table(name = "pms_merchant_user")
public class MerchantUser extends BaseEntityGen {
    /**所属商家**/
    private Merchant merchant;
    /**用户名**/
    private String username;
    /**姓名**/
    private String name;
    /**用户职称**/
    private String title;
    /**用户性别**/
    private String sex;
    /**用户手机**/
    private String phone;
    /**用户角色**/
    private String roles;
    /**用户头像**/
    private String head;
    /**用户是否锁定**/
    private boolean locked;

    @ManyToOne
    public Merchant getMerchant() {
        return merchant;
    }

    public void setMerchant(Merchant merchant) {
        this.merchant = merchant;
    }

    @Type(type = "string")
    @Length(min = 6, max = 16)
    @Column(nullable = false)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Type(type = "string")
    @Length(min = 1, max = 16)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Type(type = "string")
    @Length(max = 32)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Type(type = "string")
    @Length(max = 2)
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Type(type = "string")
    @Length(max = 32)
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Type(type = "string")
    @Length(max = 128)
    public String getRoles() {
        return roles;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }

    @Type(type = "string")
    @Length(max = 128)
    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public boolean isLocked() {
        return locked;
    }

    public boolean getLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    @JsonIgnore
    @Transient
    public List<String> getRoleList() {
        // 角色列表在数据库中实际以逗号分隔字符串存储，因此返回不能修改的List.
        return ImmutableList.copyOf(StringUtils.split(roles, ","));
    }
}
